import Vue from 'vue'
import VueRouter from 'vue-router'
// 这里要导入
import store from '../store'
const originalPush = VueRouter.prototype.push;

VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch((err) => err);
};
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'login',
    component: () => import('../views/login.vue')
  },
  {
    path: '/management',
    name: 'management',
    redirect: "/manageuser",
    component: () => import('../views/management.vue'),
    children: [
      {
        path: '/manageblock',
        component: () => import('../components/manageblock.vue')
      },
      {
        path: '/manageschool',
        component: () => import('../components/manageschool.vue')
      },
      {
        path: '/manageuser',
        component: () => import('../components/manageuser.vue')
      }
    ]
  },
  {
    path: '/admin',
    name: 'admin',
    component: () => import(/* webpackChunkName: "about" */ '../components/admin.vue')
  },
  {
    path: '/user',
    name: 'user',
    component: () => import(/* webpackChunkName: "about" */ '../components/user.vue')
  },
  {
    path: '*',
    redirect: '/'
  }
]

const router = new VueRouter({
  mode: 'history',
  // base: process.env.BASE_URL,
  routes
})

// 跳转之前
router.beforeEach((to, from, next) => {
  console.log(store.state.userstatus);
  if (store.state.userstatus === "user" || "admin") {
    next()
  } else {
    this.$router.push('/')
  }
})

// 跳转之后
// router.afterEach((to, from) => {
//   console.log(22);
// })


export default router
